var constant = require('../common/constant')

clientInterceptor.$inject = ['$q','$location'];
function clientInterceptor($q,$location){
    console.log('clientInterceptor');

    var httpInterceptor = {
        request:function(config){
            return config;
        },
        requestError:function(err){
            return $q.reject(err);
        },
        response:function(res) {
            console.log('interceptor->'+res.data);
            var data = res.data;
            if(data.code==constant.noLogin){
                $location.path('/');
                return $q.reject(res);
            }else if(data.code==constant.noPass){
                console.log('not pass');
                $location.path('/noPass');
                return $q.reject(res);
            }else if(data.code==constant.beBan){
                console.log('you ban');
                $location.path('/error');
                return $q.reject(res);
            }
            return res;
        },
        responseError:function(res){
            return $q.reject(res);
        }
    }

    return httpInterceptor;
}

module.exports = clientInterceptor;